package com.xc.freeapp.account.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import com.xc.freeapp.bean.AuthInfo;
import com.xc.freeapp.common.CheckUtils;
import com.xc.freeapp.common.ResponseBean;
import com.xc.freeapp.common.ResponsePage;
import com.xc.freeapp.entity.Messages;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.service.MessagesService;
import com.xc.freeapp.util.AuthUtil;
import com.xc.freeapp.util.Constant;
import com.xc.freeapp.util.TokenAnnotation;

@Controller
@RequestMapping("/notice")
@Api(value="/notice",description = "公告")
public class MessageController extends AbstractController {

	@Autowired
	private MessagesService messagesservice;
	/**
	 * 消息
	 * 
	 * @throws BaseException
	 */
	@TokenAnnotation(required=false)
	@RequestMapping(value = "", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "公告记录")
	public ResponsePage<List<Messages>> getMessage(@RequestParam(value = "page", required = false, defaultValue="0") Integer page,
			@RequestParam(value = "limit", required = false, defaultValue="15") Integer limit) throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		log.info("消息记录" + authInfo.getUserId() + "访问");
		PageBounds pageBounds = new PageBounds(page, limit);
		List<Messages> messages = messagesservice.getNotice(authInfo.getAppIntId(), authInfo.getUserIntId(), pageBounds);
		Paginator paginator =((PageList)messages).getPaginator();
		return new ResponsePage<List<Messages>>(messages, paginator);
	}
	public MessagesService getMessagesservice() {
		return messagesservice;
	}
	public void setMessagesservice(MessagesService messagesservice) {
		this.messagesservice = messagesservice;
	}
	/**
	 * 消息
	 * 
	 * @throws BaseException
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "{mid}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "公告记录")
	public Messages getMessage(@PathVariable("mid") Integer mid) throws BaseException {
 		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());

		log.info("消息记录" + authInfo.getUserId() + "访问");
		Messages messages = messagesservice.getMessageByid(mid);

		return messages;
	}

//	/**
//	 * 消息删除
//	 * 
//	 * @throws BaseException
//	 */
//	@TokenAnnotation(required=true)
//	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
//	@ResponseBody
//	@ApiOperation(value = "公告删除")
	public ResponseBean delMessage(@PathVariable("id") String id)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());

		CheckUtils.notBlankByName(Constant.VER_NOTNULL_BYNAME, id, "id");
		log.info("消息记录删除" + authInfo.getUserId() + "访问");
		messagesservice.delmessages(id);

		return ResponseBean.getSuccess();
	}
//	/**
//	 * 消息删除
//	 * 
//	 * @throws BaseException
//	 */
//	@TokenAnnotation(required=true)
//	@RequestMapping(value = "deletelist", method = RequestMethod.PUT)
//	@ResponseBody
//	@ApiOperation(value = "消息删除多条")
	public ResponseBean delMessages(@RequestBody List<String> messagesList)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());

		log.info("消息记录删除" + authInfo.getUserId() + "访问");
		for (String messages : messagesList) {
			messagesservice.delmessages(messages);
		}
		
		return ResponseBean.getSuccess();
	}

//	/**
//	 * 修改消息状态
//	 * 
//	 * @throws BaseException
//	 */
//	@TokenAnnotation(required=true)
//	@RequestMapping(value = "{id}", method = RequestMethod.PUT)
//	@ResponseBody
//	@ApiOperation(value = "修改消息状态")
	public ResponseBean upaMessage(@PathVariable("id") String id)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		messagesservice.updamessagesstatus(id);
		return ResponseBean.getSuccess();
	}
//	/**
//	 * 修改消息状态
//	 * 
//	 * @throws BaseException
//	 */
//	@TokenAnnotation(required=true)
//	@RequestMapping(value = "updatelist", method = RequestMethod.PUT)
//	@ResponseBody
//	@ApiOperation(value = "修改消息状态多条")
	public ResponseBean upaMessages(@RequestBody List<String> messagesList)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		for (String messages : messagesList) {
			messagesservice.updamessagesstatus(messages);
		}
		
		return ResponseBean.getSuccess();
	}


}
